package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysCompany;
import com.ruoyi.system.domain.SysPost;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPurpose;
import com.ruoyi.system.service.ISysPurposeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用途信息Controller
 * 
 * @author ruoyi
 * @date 2024-11-13
 */
@RestController
@RequestMapping("/system/purpose")
public class SysPurposeController extends BaseController
{
    @Autowired
    private ISysPurposeService sysPurposeService;

    /**
     * 查询用途信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:purpose:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPurpose sysPurpose)
    {
        startPage();
        List<SysPurpose> list = sysPurposeService.selectSysPurposeList(sysPurpose);
        return getDataTable(list);
    }

    /**
     * 导出用途信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:purpose:export')")
    @Log(title = "用途信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPurpose sysPurpose)
    {
        List<SysPurpose> list = sysPurposeService.selectSysPurposeList(sysPurpose);
        ExcelUtil<SysPurpose> util = new ExcelUtil<SysPurpose>(SysPurpose.class);
        util.exportExcel(response, list, "用途信息数据");
    }

    /**
     * 获取用途信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:purpose:query')")
    @GetMapping(value = "/{purposeId}")
    public AjaxResult getInfo(@PathVariable("purposeId") Long purposeId)
    {
        return success(sysPurposeService.selectSysPurposeByPurposeId(purposeId));
    }

    /**
     * 新增用途信息
     */
    @PreAuthorize("@ss.hasPermi('system:purpose:add')")
    @Log(title = "用途信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPurpose sysPurpose)
    {
        return toAjax(sysPurposeService.insertSysPurpose(sysPurpose));
    }

    /**
     * 修改用途信息
     */
    @PreAuthorize("@ss.hasPermi('system:purpose:edit')")
    @Log(title = "用途信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPurpose sysPurpose)
    {
        return toAjax(sysPurposeService.updateSysPurpose(sysPurpose));
    }

    /**
     * 删除用途信息
     */
    @PreAuthorize("@ss.hasPermi('system:purpose:remove')")
    @Log(title = "用途信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purposeIds}")
    public AjaxResult remove(@PathVariable Long[] purposeIds)
    {
        return toAjax(sysPurposeService.deleteSysPurposeByPurposeIds(purposeIds));
    }

    /**
     * 获取用途选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysPurpose> purposes = sysPurposeService.selectPurposeAll();
        return success(purposes);
    }

    @GetMapping("/select")
    public AjaxResult select()
    {
        List<SysPurpose> lists = sysPurposeService.selectPurposeAll();
        List<String> list = new ArrayList<>(); // 创建一个字符串列表来存储purposeName
        for (SysPurpose purpose : lists) { // 遍历lists
            list.add(purpose.getPurposeName()); // 将每个purposeName添加到list中
        }
        return AjaxResult.success(list); // 返回包含所有purposeName的list
    }
}
